package com.example.xpvideo.service.serviceImpl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.xpvideo.entity.LsHotTradingArea;
import com.example.xpvideo.entity.ResponseResult;
import com.example.xpvideo.enums.ErrCodeEnum;
import com.example.xpvideo.mapper.LsHotTradingAreaMapper;
import com.example.xpvideo.service.FileService;
import com.example.xpvideo.service.LsHotTradingAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 */
@Service
public class LsHotTradingAreaServiceImpl extends ServiceImpl<LsHotTradingAreaMapper, LsHotTradingArea>
    implements LsHotTradingAreaService{
    @Autowired
    private FileService fileService;

    @Autowired
    private LsHotTradingAreaMapper hotTradingAreaMapper;

    @Override
    public ResponseResult addOrUpdateHotTrading(LsHotTradingArea hotTradingArea, MultipartFile file) {
        if (null == hotTradingArea.getId()) {
            // 添加
            if (null == file) {
                return ResponseResult.responseResult(ErrCodeEnum.unknown_10001, "请上传商圈图片");
            }
            String url = fileService.addImages(file);
            hotTradingArea.setImages(url);
            int insert = hotTradingAreaMapper.insert(hotTradingArea);
            return insert > 0 ? ResponseResult.responseResult(0) : ResponseResult.responseResult(ErrCodeEnum.unknown_20002, null);
        } else {
            // 修改操作
            if (null != file) {
                String url = fileService.addImages(file);
                hotTradingArea.setImages(url);
            }
            int i = hotTradingAreaMapper.updateById(hotTradingArea);
            return i > 0 ? ResponseResult.responseResult(0) : ResponseResult.responseResult(ErrCodeEnum.unknown_20002, null);
        }
    }

    @Override
    @Transactional(rollbackFor = {Exception.class,RuntimeException.class})
    public ResponseResult delHotTrading(Integer id) {
        LsHotTradingArea byId = hotTradingAreaMapper.selectById(id);
        String images = byId.getImages();
        fileService.delImages(images);
        int b = hotTradingAreaMapper.deleteById(id);
        return b>0 ? ResponseResult.responseResult(0) : ResponseResult.responseResult(ErrCodeEnum.unknown_20000, null);
    }
}




